{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#import packages\n",
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df = pd.read_excel('bank.xlsx') #Read excel file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>job</th>\n",
       "      <th>marital</th>\n",
       "      <th>education</th>\n",
       "      <th>default</th>\n",
       "      <th>balance</th>\n",
       "      <th>housing</th>\n",
       "      <th>loan</th>\n",
       "      <th>contact</th>\n",
       "      <th>day</th>\n",
       "      <th>month</th>\n",
       "      <th>duration</th>\n",
       "      <th>campaign</th>\n",
       "      <th>pdays</th>\n",
       "      <th>previous</th>\n",
       "      <th>poutcome</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30</td>\n",
       "      <td>unemployed</td>\n",
       "      <td>married</td>\n",
       "      <td>primary</td>\n",
       "      <td>no</td>\n",
       "      <td>1787</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>19</td>\n",
       "      <td>oct</td>\n",
       "      <td>79</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>33</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>4789</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>11</td>\n",
       "      <td>may</td>\n",
       "      <td>220</td>\n",
       "      <td>1</td>\n",
       "      <td>339</td>\n",
       "      <td>4</td>\n",
       "      <td>failure</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>management</td>\n",
       "      <td>single</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1350</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>16</td>\n",
       "      <td>apr</td>\n",
       "      <td>185</td>\n",
       "      <td>1</td>\n",
       "      <td>330</td>\n",
       "      <td>1</td>\n",
       "      <td>failure</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>management</td>\n",
       "      <td>married</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1476</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>unknown</td>\n",
       "      <td>3</td>\n",
       "      <td>jun</td>\n",
       "      <td>199</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>0</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>unknown</td>\n",
       "      <td>5</td>\n",
       "      <td>may</td>\n",
       "      <td>226</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>no</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age          job  marital  education default  balance housing loan  \\\n",
       "0   30   unemployed  married    primary      no     1787      no   no   \n",
       "1   33     services  married  secondary      no     4789     yes  yes   \n",
       "2   35   management   single   tertiary      no     1350     yes   no   \n",
       "3   30   management  married   tertiary      no     1476     yes  yes   \n",
       "4   59  blue-collar  married  secondary      no        0     yes   no   \n",
       "\n",
       "    contact  day month  duration  campaign  pdays  previous poutcome   y  \n",
       "0  cellular   19   oct        79         1     -1         0  unknown  no  \n",
       "1  cellular   11   may       220         1    339         4  failure  no  \n",
       "2  cellular   16   apr       185         1    330         1  failure  no  \n",
       "3   unknown    3   jun       199         4     -1         0  unknown  no  \n",
       "4   unknown    5   may       226         1     -1         0  unknown  no  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 4521 entries, 0 to 4520\n",
      "Data columns (total 17 columns):\n",
      " #   Column     Non-Null Count  Dtype \n",
      "---  ------     --------------  ----- \n",
      " 0   age        4521 non-null   int64 \n",
      " 1   job        4521 non-null   object\n",
      " 2   marital    4521 non-null   object\n",
      " 3   education  4521 non-null   object\n",
      " 4   default    4521 non-null   object\n",
      " 5   balance    4521 non-null   int64 \n",
      " 6   housing    4521 non-null   object\n",
      " 7   loan       4521 non-null   object\n",
      " 8   contact    4521 non-null   object\n",
      " 9   day        4521 non-null   int64 \n",
      " 10  month      4521 non-null   object\n",
      " 11  duration   4521 non-null   int64 \n",
      " 12  campaign   4521 non-null   int64 \n",
      " 13  pdays      4521 non-null   int64 \n",
      " 14  previous   4521 non-null   int64 \n",
      " 15  poutcome   4521 non-null   object\n",
      " 16  y          4521 non-null   object\n",
      "dtypes: int64(7), object(10)\n",
      "memory usage: 600.6+ KB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "no     4000\n",
       "yes     521\n",
       "Name: y, dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['y'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['target'] = df['y'].apply(lambda x : 1 if x == 'yes' else 0)  # Convert to numeric\n",
    "df = df.drop('y',axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# WOE and IV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import packages\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pandas.core.algorithms as algos\n",
    "from pandas import Series\n",
    "import scipy.stats.stats as stats\n",
    "import re\n",
    "import traceback\n",
    "import string\n",
    "\n",
    "max_bin = 20\n",
    "force_bin = 3\n",
    "\n",
    "# define a binning function\n",
    "def mono_bin(Y, X, n = max_bin):\n",
    "    \n",
    "    df1 = pd.DataFrame({\"X\": X, \"Y\": Y})\n",
    "    justmiss = df1[['X','Y']][df1.X.isnull()]\n",
    "    notmiss = df1[['X','Y']][df1.X.notnull()]\n",
    "    r = 0\n",
    "    while np.abs(r) < 1:\n",
    "        try:\n",
    "            d1 = pd.DataFrame({\"X\": notmiss.X, \"Y\": notmiss.Y, \"Bucket\": pd.qcut(notmiss.X, n)})\n",
    "            d2 = d1.groupby('Bucket', as_index=True)\n",
    "            r, p = stats.spearmanr(d2.mean().X, d2.mean().Y)\n",
    "            n = n - 1 \n",
    "        except Exception as e:\n",
    "            n = n - 1\n",
    "\n",
    "    if len(d2) == 1:\n",
    "        n = force_bin         \n",
    "        bins = algos.quantile(notmiss.X, np.linspace(0, 1, n))\n",
    "        if len(np.unique(bins)) == 2:\n",
    "            bins = np.insert(bins, 0, 1)\n",
    "            bins[1] = bins[1]-(bins[1]/2)\n",
    "        d1 = pd.DataFrame({\"X\": notmiss.X, \"Y\": notmiss.Y, \"Bucket\": pd.cut(notmiss.X, np.unique(bins),include_lowest=True)}) \n",
    "        d2 = d1.groupby('Bucket', as_index=True)\n",
    "    \n",
    "    d3 = pd.DataFrame({},index=[])\n",
    "    d3[\"MIN_VALUE\"] = d2.min().X\n",
    "    d3[\"MAX_VALUE\"] = d2.max().X\n",
    "    d3[\"COUNT\"] = d2.count().Y\n",
    "    d3[\"EVENT\"] = d2.sum().Y\n",
    "    d3[\"NONEVENT\"] = d2.count().Y - d2.sum().Y\n",
    "    d3=d3.reset_index(drop=True)\n",
    "    \n",
    "    if len(justmiss.index) > 0:\n",
    "        d4 = pd.DataFrame({'MIN_VALUE':np.nan},index=[0])\n",
    "        d4[\"MAX_VALUE\"] = np.nan\n",
    "        d4[\"COUNT\"] = justmiss.count().Y\n",
    "        d4[\"EVENT\"] = justmiss.sum().Y\n",
    "        d4[\"NONEVENT\"] = justmiss.count().Y - justmiss.sum().Y\n",
    "        d3 = d3.append(d4,ignore_index=True)\n",
    "    \n",
    "    d3[\"EVENT_RATE\"] = d3.EVENT/d3.COUNT\n",
    "    d3[\"NON_EVENT_RATE\"] = d3.NONEVENT/d3.COUNT\n",
    "    d3[\"DIST_EVENT\"] = d3.EVENT/d3.sum().EVENT\n",
    "    d3[\"DIST_NON_EVENT\"] = d3.NONEVENT/d3.sum().NONEVENT\n",
    "    d3[\"WOE\"] = np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)\n",
    "    d3[\"IV\"] = (d3.DIST_EVENT-d3.DIST_NON_EVENT)*np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)\n",
    "    d3[\"VAR_NAME\"] = \"VAR\"\n",
    "    d3 = d3[['VAR_NAME','MIN_VALUE', 'MAX_VALUE', 'COUNT', 'EVENT', 'EVENT_RATE', 'NONEVENT', 'NON_EVENT_RATE', 'DIST_EVENT','DIST_NON_EVENT','WOE', 'IV']]       \n",
    "    d3 = d3.replace([np.inf, -np.inf], 0)\n",
    "    d3.IV = d3.IV.sum()\n",
    "    \n",
    "    return(d3)\n",
    "\n",
    "def char_bin(Y, X):\n",
    "        \n",
    "    df1 = pd.DataFrame({\"X\": X, \"Y\": Y})\n",
    "    justmiss = df1[['X','Y']][df1.X.isnull()]\n",
    "    notmiss = df1[['X','Y']][df1.X.notnull()]    \n",
    "    df2 = notmiss.groupby('X',as_index=True)\n",
    "    \n",
    "    d3 = pd.DataFrame({},index=[])\n",
    "    d3[\"COUNT\"] = df2.count().Y\n",
    "    d3[\"MIN_VALUE\"] = df2.sum().Y.index\n",
    "    d3[\"MAX_VALUE\"] = d3[\"MIN_VALUE\"]\n",
    "    d3[\"EVENT\"] = df2.sum().Y\n",
    "    d3[\"NONEVENT\"] = df2.count().Y - df2.sum().Y\n",
    "    \n",
    "    if len(justmiss.index) > 0:\n",
    "        d4 = pd.DataFrame({'MIN_VALUE':np.nan},index=[0])\n",
    "        d4[\"MAX_VALUE\"] = np.nan\n",
    "        d4[\"COUNT\"] = justmiss.count().Y\n",
    "        d4[\"EVENT\"] = justmiss.sum().Y\n",
    "        d4[\"NONEVENT\"] = justmiss.count().Y - justmiss.sum().Y\n",
    "        d3 = d3.append(d4,ignore_index=True)\n",
    "    \n",
    "    d3[\"EVENT_RATE\"] = d3.EVENT/d3.COUNT\n",
    "    d3[\"NON_EVENT_RATE\"] = d3.NONEVENT/d3.COUNT\n",
    "    d3[\"DIST_EVENT\"] = d3.EVENT/d3.sum().EVENT\n",
    "    d3[\"DIST_NON_EVENT\"] = d3.NONEVENT/d3.sum().NONEVENT\n",
    "    d3[\"WOE\"] = np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)\n",
    "    d3[\"IV\"] = (d3.DIST_EVENT-d3.DIST_NON_EVENT)*np.log(d3.DIST_EVENT/d3.DIST_NON_EVENT)\n",
    "    d3[\"VAR_NAME\"] = \"VAR\"\n",
    "    d3 = d3[['VAR_NAME','MIN_VALUE', 'MAX_VALUE', 'COUNT', 'EVENT', 'EVENT_RATE', 'NONEVENT', 'NON_EVENT_RATE', 'DIST_EVENT','DIST_NON_EVENT','WOE', 'IV']]      \n",
    "    d3 = d3.replace([np.inf, -np.inf], 0)\n",
    "    d3.IV = d3.IV.sum()\n",
    "    d3 = d3.reset_index(drop=True)\n",
    "    \n",
    "    return(d3)\n",
    "\n",
    "def data_vars(df1, target):\n",
    "    \n",
    "    stack = traceback.extract_stack()\n",
    "    filename, lineno, function_name, code = stack[-2]\n",
    "    vars_name = re.compile(r'\\((.*?)\\).*$').search(code).groups()[0]\n",
    "    final = (re.findall(r\"[\\w']+\", vars_name))[-1]\n",
    "    \n",
    "    x = df1.dtypes.index\n",
    "    count = -1\n",
    "    \n",
    "    for i in x:\n",
    "        if i.upper() not in (final.upper()):\n",
    "            if np.issubdtype(df1[i], np.number) and len(Series.unique(df1[i])) > 2:\n",
    "                conv = mono_bin(target, df1[i])\n",
    "                conv[\"VAR_NAME\"] = i\n",
    "                count = count + 1\n",
    "            else:\n",
    "                conv = char_bin(target, df1[i])\n",
    "                conv[\"VAR_NAME\"] = i            \n",
    "                count = count + 1\n",
    "                \n",
    "            if count == 0:\n",
    "                iv_df = conv\n",
    "            else:\n",
    "                iv_df = iv_df.append(conv,ignore_index=True)\n",
    "    \n",
    "    iv = pd.DataFrame({'IV':iv_df.groupby('VAR_NAME').IV.max()})\n",
    "    iv = iv.reset_index()\n",
    "    return(iv_df,iv) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>job</th>\n",
       "      <th>marital</th>\n",
       "      <th>education</th>\n",
       "      <th>default</th>\n",
       "      <th>balance</th>\n",
       "      <th>housing</th>\n",
       "      <th>loan</th>\n",
       "      <th>contact</th>\n",
       "      <th>day</th>\n",
       "      <th>month</th>\n",
       "      <th>duration</th>\n",
       "      <th>campaign</th>\n",
       "      <th>pdays</th>\n",
       "      <th>previous</th>\n",
       "      <th>poutcome</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30</td>\n",
       "      <td>unemployed</td>\n",
       "      <td>married</td>\n",
       "      <td>primary</td>\n",
       "      <td>no</td>\n",
       "      <td>1787</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>19</td>\n",
       "      <td>oct</td>\n",
       "      <td>79</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>33</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>4789</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>11</td>\n",
       "      <td>may</td>\n",
       "      <td>220</td>\n",
       "      <td>1</td>\n",
       "      <td>339</td>\n",
       "      <td>4</td>\n",
       "      <td>failure</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>management</td>\n",
       "      <td>single</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1350</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>16</td>\n",
       "      <td>apr</td>\n",
       "      <td>185</td>\n",
       "      <td>1</td>\n",
       "      <td>330</td>\n",
       "      <td>1</td>\n",
       "      <td>failure</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>management</td>\n",
       "      <td>married</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1476</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>unknown</td>\n",
       "      <td>3</td>\n",
       "      <td>jun</td>\n",
       "      <td>199</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>0</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>unknown</td>\n",
       "      <td>5</td>\n",
       "      <td>may</td>\n",
       "      <td>226</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4516</th>\n",
       "      <td>33</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>-333</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>30</td>\n",
       "      <td>jul</td>\n",
       "      <td>329</td>\n",
       "      <td>5</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4517</th>\n",
       "      <td>57</td>\n",
       "      <td>self-employed</td>\n",
       "      <td>married</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>yes</td>\n",
       "      <td>-3313</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>unknown</td>\n",
       "      <td>9</td>\n",
       "      <td>may</td>\n",
       "      <td>153</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4518</th>\n",
       "      <td>57</td>\n",
       "      <td>technician</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>295</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>19</td>\n",
       "      <td>aug</td>\n",
       "      <td>151</td>\n",
       "      <td>11</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>unknown</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4519</th>\n",
       "      <td>28</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>1137</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>6</td>\n",
       "      <td>feb</td>\n",
       "      <td>129</td>\n",
       "      <td>4</td>\n",
       "      <td>211</td>\n",
       "      <td>3</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4520</th>\n",
       "      <td>44</td>\n",
       "      <td>entrepreneur</td>\n",
       "      <td>single</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1136</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>3</td>\n",
       "      <td>apr</td>\n",
       "      <td>345</td>\n",
       "      <td>2</td>\n",
       "      <td>249</td>\n",
       "      <td>7</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4521 rows × 17 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age            job  marital  education default  balance housing loan  \\\n",
       "0      30     unemployed  married    primary      no     1787      no   no   \n",
       "1      33       services  married  secondary      no     4789     yes  yes   \n",
       "2      35     management   single   tertiary      no     1350     yes   no   \n",
       "3      30     management  married   tertiary      no     1476     yes  yes   \n",
       "4      59    blue-collar  married  secondary      no        0     yes   no   \n",
       "...   ...            ...      ...        ...     ...      ...     ...  ...   \n",
       "4516   33       services  married  secondary      no     -333     yes   no   \n",
       "4517   57  self-employed  married   tertiary     yes    -3313     yes  yes   \n",
       "4518   57     technician  married  secondary      no      295      no   no   \n",
       "4519   28    blue-collar  married  secondary      no     1137      no   no   \n",
       "4520   44   entrepreneur   single   tertiary      no     1136     yes  yes   \n",
       "\n",
       "       contact  day month  duration  campaign  pdays  previous poutcome  \\\n",
       "0     cellular   19   oct        79         1     -1         0  unknown   \n",
       "1     cellular   11   may       220         1    339         4  failure   \n",
       "2     cellular   16   apr       185         1    330         1  failure   \n",
       "3      unknown    3   jun       199         4     -1         0  unknown   \n",
       "4      unknown    5   may       226         1     -1         0  unknown   \n",
       "...        ...  ...   ...       ...       ...    ...       ...      ...   \n",
       "4516  cellular   30   jul       329         5     -1         0  unknown   \n",
       "4517   unknown    9   may       153         1     -1         0  unknown   \n",
       "4518  cellular   19   aug       151        11     -1         0  unknown   \n",
       "4519  cellular    6   feb       129         4    211         3    other   \n",
       "4520  cellular    3   apr       345         2    249         7    other   \n",
       "\n",
       "      target  \n",
       "0          0  \n",
       "1          0  \n",
       "2          0  \n",
       "3          0  \n",
       "4          0  \n",
       "...      ...  \n",
       "4516       0  \n",
       "4517       0  \n",
       "4518       0  \n",
       "4519       0  \n",
       "4520       0  \n",
       "\n",
       "[4521 rows x 17 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "final_iv, IV = data_vars(df,df.target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VAR_NAME</th>\n",
       "      <th>MIN_VALUE</th>\n",
       "      <th>MAX_VALUE</th>\n",
       "      <th>COUNT</th>\n",
       "      <th>EVENT</th>\n",
       "      <th>EVENT_RATE</th>\n",
       "      <th>NONEVENT</th>\n",
       "      <th>NON_EVENT_RATE</th>\n",
       "      <th>DIST_EVENT</th>\n",
       "      <th>DIST_NON_EVENT</th>\n",
       "      <th>WOE</th>\n",
       "      <th>IV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>age</td>\n",
       "      <td>19</td>\n",
       "      <td>39</td>\n",
       "      <td>2290</td>\n",
       "      <td>259</td>\n",
       "      <td>0.113100</td>\n",
       "      <td>2031</td>\n",
       "      <td>0.886900</td>\n",
       "      <td>0.497121</td>\n",
       "      <td>0.50775</td>\n",
       "      <td>-0.021156</td>\n",
       "      <td>0.000452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>age</td>\n",
       "      <td>40</td>\n",
       "      <td>87</td>\n",
       "      <td>2231</td>\n",
       "      <td>262</td>\n",
       "      <td>0.117436</td>\n",
       "      <td>1969</td>\n",
       "      <td>0.882564</td>\n",
       "      <td>0.502879</td>\n",
       "      <td>0.49225</td>\n",
       "      <td>0.021363</td>\n",
       "      <td>0.000452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>job</td>\n",
       "      <td>admin.</td>\n",
       "      <td>admin.</td>\n",
       "      <td>478</td>\n",
       "      <td>58</td>\n",
       "      <td>0.121339</td>\n",
       "      <td>420</td>\n",
       "      <td>0.878661</td>\n",
       "      <td>0.111324</td>\n",
       "      <td>0.10500</td>\n",
       "      <td>0.058488</td>\n",
       "      <td>0.132519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>job</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>946</td>\n",
       "      <td>69</td>\n",
       "      <td>0.072939</td>\n",
       "      <td>877</td>\n",
       "      <td>0.927061</td>\n",
       "      <td>0.132438</td>\n",
       "      <td>0.21925</td>\n",
       "      <td>-0.504101</td>\n",
       "      <td>0.132519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>job</td>\n",
       "      <td>entrepreneur</td>\n",
       "      <td>entrepreneur</td>\n",
       "      <td>168</td>\n",
       "      <td>15</td>\n",
       "      <td>0.089286</td>\n",
       "      <td>153</td>\n",
       "      <td>0.910714</td>\n",
       "      <td>0.028791</td>\n",
       "      <td>0.03825</td>\n",
       "      <td>-0.284088</td>\n",
       "      <td>0.132519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>previous</td>\n",
       "      <td>2</td>\n",
       "      <td>25</td>\n",
       "      <td>530</td>\n",
       "      <td>133</td>\n",
       "      <td>0.250943</td>\n",
       "      <td>397</td>\n",
       "      <td>0.749057</td>\n",
       "      <td>0.255278</td>\n",
       "      <td>0.09925</td>\n",
       "      <td>0.944712</td>\n",
       "      <td>0.177081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>poutcome</td>\n",
       "      <td>failure</td>\n",
       "      <td>failure</td>\n",
       "      <td>490</td>\n",
       "      <td>63</td>\n",
       "      <td>0.128571</td>\n",
       "      <td>427</td>\n",
       "      <td>0.871429</td>\n",
       "      <td>0.120921</td>\n",
       "      <td>0.10675</td>\n",
       "      <td>0.124650</td>\n",
       "      <td>0.461890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>poutcome</td>\n",
       "      <td>other</td>\n",
       "      <td>other</td>\n",
       "      <td>197</td>\n",
       "      <td>38</td>\n",
       "      <td>0.192893</td>\n",
       "      <td>159</td>\n",
       "      <td>0.807107</td>\n",
       "      <td>0.072937</td>\n",
       "      <td>0.03975</td>\n",
       "      <td>0.606982</td>\n",
       "      <td>0.461890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>poutcome</td>\n",
       "      <td>success</td>\n",
       "      <td>success</td>\n",
       "      <td>129</td>\n",
       "      <td>83</td>\n",
       "      <td>0.643411</td>\n",
       "      <td>46</td>\n",
       "      <td>0.356589</td>\n",
       "      <td>0.159309</td>\n",
       "      <td>0.01150</td>\n",
       "      <td>2.628499</td>\n",
       "      <td>0.461890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>poutcome</td>\n",
       "      <td>unknown</td>\n",
       "      <td>unknown</td>\n",
       "      <td>3705</td>\n",
       "      <td>337</td>\n",
       "      <td>0.090958</td>\n",
       "      <td>3368</td>\n",
       "      <td>0.909042</td>\n",
       "      <td>0.646833</td>\n",
       "      <td>0.84200</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>0.461890</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>68 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    VAR_NAME     MIN_VALUE     MAX_VALUE  COUNT  EVENT  EVENT_RATE  NONEVENT  \\\n",
       "0        age            19            39   2290    259    0.113100      2031   \n",
       "1        age            40            87   2231    262    0.117436      1969   \n",
       "2        job        admin.        admin.    478     58    0.121339       420   \n",
       "3        job   blue-collar   blue-collar    946     69    0.072939       877   \n",
       "4        job  entrepreneur  entrepreneur    168     15    0.089286       153   \n",
       "..       ...           ...           ...    ...    ...         ...       ...   \n",
       "63  previous             2            25    530    133    0.250943       397   \n",
       "64  poutcome       failure       failure    490     63    0.128571       427   \n",
       "65  poutcome         other         other    197     38    0.192893       159   \n",
       "66  poutcome       success       success    129     83    0.643411        46   \n",
       "67  poutcome       unknown       unknown   3705    337    0.090958      3368   \n",
       "\n",
       "    NON_EVENT_RATE  DIST_EVENT  DIST_NON_EVENT       WOE        IV  \n",
       "0         0.886900    0.497121         0.50775 -0.021156  0.000452  \n",
       "1         0.882564    0.502879         0.49225  0.021363  0.000452  \n",
       "2         0.878661    0.111324         0.10500  0.058488  0.132519  \n",
       "3         0.927061    0.132438         0.21925 -0.504101  0.132519  \n",
       "4         0.910714    0.028791         0.03825 -0.284088  0.132519  \n",
       "..             ...         ...             ...       ...       ...  \n",
       "63        0.749057    0.255278         0.09925  0.944712  0.177081  \n",
       "64        0.871429    0.120921         0.10675  0.124650  0.461890  \n",
       "65        0.807107    0.072937         0.03975  0.606982  0.461890  \n",
       "66        0.356589    0.159309         0.01150  2.628499  0.461890  \n",
       "67        0.909042    0.646833         0.84200 -0.263692  0.461890  \n",
       "\n",
       "[68 rows x 12 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_iv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VAR_NAME</th>\n",
       "      <th>IV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>default</td>\n",
       "      <td>0.000016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>age</td>\n",
       "      <td>0.000452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>day</td>\n",
       "      <td>0.004581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>campaign</td>\n",
       "      <td>0.023342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>education</td>\n",
       "      <td>0.031812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>marital</td>\n",
       "      <td>0.040090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>loan</td>\n",
       "      <td>0.060791</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>balance</td>\n",
       "      <td>0.076208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>housing</td>\n",
       "      <td>0.106556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>job</td>\n",
       "      <td>0.132519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>previous</td>\n",
       "      <td>0.177081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>pdays</td>\n",
       "      <td>0.203267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>contact</td>\n",
       "      <td>0.247762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>month</td>\n",
       "      <td>0.379533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>poutcome</td>\n",
       "      <td>0.461890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>duration</td>\n",
       "      <td>1.651501</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     VAR_NAME        IV\n",
       "5     default  0.000016\n",
       "0         age  0.000452\n",
       "4         day  0.004581\n",
       "2    campaign  0.023342\n",
       "7   education  0.031812\n",
       "11    marital  0.040090\n",
       "10       loan  0.060791\n",
       "1     balance  0.076208\n",
       "8     housing  0.106556\n",
       "9         job  0.132519\n",
       "15   previous  0.177081\n",
       "13      pdays  0.203267\n",
       "3     contact  0.247762\n",
       "12      month  0.379533\n",
       "14   poutcome  0.461890\n",
       "6    duration  1.651501"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果需要做特征选择，可以根据一定阈值取前N个特征\n",
    "IV.sort_values('IV')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "IV.to_csv('test.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Apply WOE values to your dataframe columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The below code snippet can be used to apply the WOE values to your dataframe columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "transform_vars_list = df.columns.difference(['target'])\n",
    "transform_prefix = 'new_' # leave this value blank if you need replace the original column values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'balance', 'campaign', 'contact', 'day', 'default', 'duration',\n",
       "       'education', 'housing', 'job', 'loan', 'marital', 'month', 'pdays',\n",
       "       'poutcome', 'previous'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transform_vars_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对字符串或者数值类型的bin进行WOE填充\n",
    "for var in transform_vars_list:\n",
    "    small_df = final_iv[final_iv['VAR_NAME'] == var]\n",
    "    transform_dict = dict(zip(small_df.MAX_VALUE,small_df.WOE))\n",
    "   #print(transform_dict)\n",
    "    replace_cmd = ''\n",
    "    replace_cmd1 = ''\n",
    "    for i in sorted(transform_dict.items()):\n",
    "        #print(i)\n",
    "        replace_cmd = replace_cmd + str(i[1]) + str(' if x <= ') + str(i[0]) + ' else '\n",
    "        replace_cmd1 = replace_cmd1 + str(i[1]) + str(' if x == \"') + str(i[0]) + '\" else '\n",
    "        #print(replace_cmd,replace_cmd1)\n",
    "    replace_cmd = replace_cmd + '0'\n",
    "    replace_cmd1 = replace_cmd1 + '0'\n",
    "    if replace_cmd != '0':\n",
    "        try:\n",
    "            df[transform_prefix + var] = df[var].apply(lambda x: eval(replace_cmd))\n",
    "        except:\n",
    "            df[transform_prefix + var] = df[var].apply(lambda x: eval(replace_cmd1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cellular     2896\n",
       "unknown      1324\n",
       "telephone     301\n",
       "Name: contact, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['contact'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 0.252971    2896\n",
       "-0.992072    1324\n",
       " 0.273413     301\n",
       "Name: new_contact, dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['new_contact'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>job</th>\n",
       "      <th>marital</th>\n",
       "      <th>education</th>\n",
       "      <th>default</th>\n",
       "      <th>balance</th>\n",
       "      <th>housing</th>\n",
       "      <th>loan</th>\n",
       "      <th>contact</th>\n",
       "      <th>day</th>\n",
       "      <th>...</th>\n",
       "      <th>new_duration</th>\n",
       "      <th>new_education</th>\n",
       "      <th>new_housing</th>\n",
       "      <th>new_job</th>\n",
       "      <th>new_loan</th>\n",
       "      <th>new_marital</th>\n",
       "      <th>new_month</th>\n",
       "      <th>new_pdays</th>\n",
       "      <th>new_poutcome</th>\n",
       "      <th>new_previous</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>30</td>\n",
       "      <td>unemployed</td>\n",
       "      <td>married</td>\n",
       "      <td>primary</td>\n",
       "      <td>no</td>\n",
       "      <td>1787</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>19</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.857594</td>\n",
       "      <td>-0.222812</td>\n",
       "      <td>0.330235</td>\n",
       "      <td>-0.141683</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>1.888017</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>33</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>4789</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>11</td>\n",
       "      <td>...</td>\n",
       "      <td>0.069240</td>\n",
       "      <td>-0.091389</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>-0.261650</td>\n",
       "      <td>-0.674391</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>-0.603059</td>\n",
       "      <td>0.793417</td>\n",
       "      <td>0.124650</td>\n",
       "      <td>0.944712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35</td>\n",
       "      <td>management</td>\n",
       "      <td>single</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1350</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>16</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.875304</td>\n",
       "      <td>0.247404</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>0.182479</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>0.219951</td>\n",
       "      <td>0.595591</td>\n",
       "      <td>0.793417</td>\n",
       "      <td>0.124650</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>management</td>\n",
       "      <td>married</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1476</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>unknown</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.875304</td>\n",
       "      <td>0.247404</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>0.182479</td>\n",
       "      <td>-0.674391</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>-0.119785</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>59</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>0</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>unknown</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.069240</td>\n",
       "      <td>-0.091389</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>-0.504101</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>-0.603059</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4516</th>\n",
       "      <td>33</td>\n",
       "      <td>services</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>-333</td>\n",
       "      <td>yes</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>30</td>\n",
       "      <td>...</td>\n",
       "      <td>0.260333</td>\n",
       "      <td>-0.091389</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>-0.261650</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>-0.320077</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4517</th>\n",
       "      <td>57</td>\n",
       "      <td>self-employed</td>\n",
       "      <td>married</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>yes</td>\n",
       "      <td>-3313</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>unknown</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.118701</td>\n",
       "      <td>0.247404</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>-0.059718</td>\n",
       "      <td>-0.674391</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>-0.603059</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4518</th>\n",
       "      <td>57</td>\n",
       "      <td>technician</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>295</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>19</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.118701</td>\n",
       "      <td>-0.091389</td>\n",
       "      <td>0.330235</td>\n",
       "      <td>-0.072279</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>0.090583</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.263692</td>\n",
       "      <td>-0.190217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4519</th>\n",
       "      <td>28</td>\n",
       "      <td>blue-collar</td>\n",
       "      <td>married</td>\n",
       "      <td>secondary</td>\n",
       "      <td>no</td>\n",
       "      <td>1137</td>\n",
       "      <td>no</td>\n",
       "      <td>no</td>\n",
       "      <td>cellular</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.118701</td>\n",
       "      <td>-0.091389</td>\n",
       "      <td>0.330235</td>\n",
       "      <td>-0.504101</td>\n",
       "      <td>0.090598</td>\n",
       "      <td>-0.169697</td>\n",
       "      <td>0.460950</td>\n",
       "      <td>0.793417</td>\n",
       "      <td>0.606982</td>\n",
       "      <td>0.944712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4520</th>\n",
       "      <td>44</td>\n",
       "      <td>entrepreneur</td>\n",
       "      <td>single</td>\n",
       "      <td>tertiary</td>\n",
       "      <td>no</td>\n",
       "      <td>1136</td>\n",
       "      <td>yes</td>\n",
       "      <td>yes</td>\n",
       "      <td>cellular</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0.260333</td>\n",
       "      <td>0.247404</td>\n",
       "      <td>-0.325552</td>\n",
       "      <td>-0.284088</td>\n",
       "      <td>-0.674391</td>\n",
       "      <td>0.219951</td>\n",
       "      <td>0.595591</td>\n",
       "      <td>0.793417</td>\n",
       "      <td>0.606982</td>\n",
       "      <td>0.944712</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4521 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age            job  marital  education default  balance housing loan  \\\n",
       "0      30     unemployed  married    primary      no     1787      no   no   \n",
       "1      33       services  married  secondary      no     4789     yes  yes   \n",
       "2      35     management   single   tertiary      no     1350     yes   no   \n",
       "3      30     management  married   tertiary      no     1476     yes  yes   \n",
       "4      59    blue-collar  married  secondary      no        0     yes   no   \n",
       "...   ...            ...      ...        ...     ...      ...     ...  ...   \n",
       "4516   33       services  married  secondary      no     -333     yes   no   \n",
       "4517   57  self-employed  married   tertiary     yes    -3313     yes  yes   \n",
       "4518   57     technician  married  secondary      no      295      no   no   \n",
       "4519   28    blue-collar  married  secondary      no     1137      no   no   \n",
       "4520   44   entrepreneur   single   tertiary      no     1136     yes  yes   \n",
       "\n",
       "       contact  day  ... new_duration  new_education  new_housing   new_job  \\\n",
       "0     cellular   19  ...    -1.857594      -0.222812     0.330235 -0.141683   \n",
       "1     cellular   11  ...     0.069240      -0.091389    -0.325552 -0.261650   \n",
       "2     cellular   16  ...    -0.875304       0.247404    -0.325552  0.182479   \n",
       "3      unknown    3  ...    -0.875304       0.247404    -0.325552  0.182479   \n",
       "4      unknown    5  ...     0.069240      -0.091389    -0.325552 -0.504101   \n",
       "...        ...  ...  ...          ...            ...          ...       ...   \n",
       "4516  cellular   30  ...     0.260333      -0.091389    -0.325552 -0.261650   \n",
       "4517   unknown    9  ...    -1.118701       0.247404    -0.325552 -0.059718   \n",
       "4518  cellular   19  ...    -1.118701      -0.091389     0.330235 -0.072279   \n",
       "4519  cellular    6  ...    -1.118701      -0.091389     0.330235 -0.504101   \n",
       "4520  cellular    3  ...     0.260333       0.247404    -0.325552 -0.284088   \n",
       "\n",
       "      new_loan new_marital  new_month  new_pdays  new_poutcome  new_previous  \n",
       "0     0.090598   -0.169697   1.888017  -0.263692     -0.263692     -0.190217  \n",
       "1    -0.674391   -0.169697  -0.603059   0.793417      0.124650      0.944712  \n",
       "2     0.090598    0.219951   0.595591   0.793417      0.124650     -0.190217  \n",
       "3    -0.674391   -0.169697  -0.119785  -0.263692     -0.263692     -0.190217  \n",
       "4     0.090598   -0.169697  -0.603059  -0.263692     -0.263692     -0.190217  \n",
       "...        ...         ...        ...        ...           ...           ...  \n",
       "4516  0.090598   -0.169697  -0.320077  -0.263692     -0.263692     -0.190217  \n",
       "4517 -0.674391   -0.169697  -0.603059  -0.263692     -0.263692     -0.190217  \n",
       "4518  0.090598   -0.169697   0.090583  -0.263692     -0.263692     -0.190217  \n",
       "4519  0.090598   -0.169697   0.460950   0.793417      0.606982      0.944712  \n",
       "4520 -0.674391    0.219951   0.595591   0.793417      0.606982      0.944712  \n",
       "\n",
       "[4521 rows x 33 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "small_df = final_iv[final_iv['VAR_NAME'] == 'contact']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VAR_NAME</th>\n",
       "      <th>MIN_VALUE</th>\n",
       "      <th>MAX_VALUE</th>\n",
       "      <th>COUNT</th>\n",
       "      <th>EVENT</th>\n",
       "      <th>EVENT_RATE</th>\n",
       "      <th>NONEVENT</th>\n",
       "      <th>NON_EVENT_RATE</th>\n",
       "      <th>DIST_EVENT</th>\n",
       "      <th>DIST_NON_EVENT</th>\n",
       "      <th>WOE</th>\n",
       "      <th>IV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>contact</td>\n",
       "      <td>cellular</td>\n",
       "      <td>cellular</td>\n",
       "      <td>2896</td>\n",
       "      <td>416</td>\n",
       "      <td>0.143646</td>\n",
       "      <td>2480</td>\n",
       "      <td>0.856354</td>\n",
       "      <td>0.798464</td>\n",
       "      <td>0.62000</td>\n",
       "      <td>0.252971</td>\n",
       "      <td>0.247762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>contact</td>\n",
       "      <td>telephone</td>\n",
       "      <td>telephone</td>\n",
       "      <td>301</td>\n",
       "      <td>44</td>\n",
       "      <td>0.146179</td>\n",
       "      <td>257</td>\n",
       "      <td>0.853821</td>\n",
       "      <td>0.084453</td>\n",
       "      <td>0.06425</td>\n",
       "      <td>0.273413</td>\n",
       "      <td>0.247762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>contact</td>\n",
       "      <td>unknown</td>\n",
       "      <td>unknown</td>\n",
       "      <td>1324</td>\n",
       "      <td>61</td>\n",
       "      <td>0.046073</td>\n",
       "      <td>1263</td>\n",
       "      <td>0.953927</td>\n",
       "      <td>0.117083</td>\n",
       "      <td>0.31575</td>\n",
       "      <td>-0.992072</td>\n",
       "      <td>0.247762</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   VAR_NAME  MIN_VALUE  MAX_VALUE  COUNT  EVENT  EVENT_RATE  NONEVENT  \\\n",
       "31  contact   cellular   cellular   2896    416    0.143646      2480   \n",
       "32  contact  telephone  telephone    301     44    0.146179       257   \n",
       "33  contact    unknown    unknown   1324     61    0.046073      1263   \n",
       "\n",
       "    NON_EVENT_RATE  DIST_EVENT  DIST_NON_EVENT       WOE        IV  \n",
       "31        0.856354    0.798464         0.62000  0.252971  0.247762  \n",
       "32        0.853821    0.084453         0.06425  0.273413  0.247762  \n",
       "33        0.953927    0.117083         0.31575 -0.992072  0.247762  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "small_df"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
